<?php

namespace app\api\controller;


use app\admin\model\BillModel;
use app\admin\model\CustomerModel;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\Validate;


class Customer extends Api
{

    protected $noNeedRight = '*';


    public function index()
    {
        $page_size = $this->request->param('page_size', 10);
        $kw = $this->request->param('kw', '');
        $sort = $this->request->param('sort');
        $sort_way = $this->request->param('sort_way', '');
        $build_id = $this->request->param('build_id/d');
        $contract_status = $this->request->param('contract_status/d');
        $type = $this->request->param('type/d');
        $where = [];
        if (!empty($kw)) {
            $where['cus.name|cus.mobile|contract.room_name'] = ['like', '%' . $kw . '%'];
        }
        if ($type > 0) {
            $where['cus.type'] = $type;
        }
        if ($build_id > 0) {
            $where['contract.build_id'] = $build_id;
        }
        if (!empty($contract_status)) {
            $where['contract.status'] = $contract_status;
        }

        if (empty($sort) || $sort != 'start_time') {
            $sort = 'cus.id';
        }
        if (empty($sort_way) || !in_array($sort_way, ['asc', 'desc'])) {
            $sort_way = 'desc';
        }

        $where['cus.user_id'] = $this->auth->id;

        $list = CustomerModel::alias('cus')
            ->join('user_contract contract', 'cus.mobile = contract.user_phone', 'left')
            ->where($where)
            ->field('cus.id,cus.name,cus.mobile,cus.is_black,cus.type,cus.sex,cus.id_card,cus.card_image,cus.contact_name,
            contract.id as contract_id,contract.status,contract.build_address,contract.room_name,contract.start_time')
            ->order($sort, $sort_way)
            ->paginate($page_size)->each(function ($item) {
                $item->wait_pay = BillModel::where('contract_id', $item->contract_id)
                    ->where('status', 1)->sum('money');
            });

        $this->success('success', $list);
    }

    public function add()
    {
        $params = $this->request->post();
        $checkRes = $this->validate($params, [
            'type|类型' => 'require|in:1,2',
            'name|姓名' => 'require',
            'sex|性别' => 'in:1,2',
            'mobile|手机号' => 'require',
        ]);
        if ($checkRes !== true) {
            $this->error($checkRes);
        }
        if (!Validate::regex($params['mobile'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $params['user_id'] = $this->auth->id;
        $count = CustomerModel::where([
            'user_id' => $params['user_id'],
            'mobile' => $params['mobile']
        ])->count();

        if ($count) {
            $this->error('该手机号已经添加过');
        }

        $res = CustomerModel::create($params, true);

        if ($res) {
            $this->success('保存成功', ['id'=>$res->id]);
        } else {
            $this->error('保存失败');
        }
    }


    public function info()
    {
        $id = $this->request->param('id/d');
        if ($id < 1) {
            $this->error('id错误');
        }
        $row = CustomerModel::get($id);
        if (empty($row)) {
            $this->error('数据不存在');
        }

        $this->success('成功', $row);
    }

    public function del()
    {
        $id = $this->request->param('id/d');
        if ($id < 1) {
            $this->error('id错误');
        }
        $row = CustomerModel::get($id);
        if (empty($row)) {
            $this->error('数据不存在');
        }
        $res = $row->delete();
        if ($res) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

    public function black_type_list()
    {
        $this->success('成功',(new CustomerModel)->getBlackTypeList());
    }
    public function black()
    {
        $id = $this->request->param('id/d');
        if ($id < 1) {
            $this->error('id错误');
        }
        $params = $this->request->post();
        $checkRes = $this->validate($params, [
            'black_type|类型' => 'require',
            'black_reason|原因' => 'require',
        ]);
        if ($checkRes !== true) {
            $this->error($checkRes);
        }
        if (!array_key_exists($params['black_type'], (new CustomerModel)->getBlackTypeList())) {
            $this->error('类型错误');
        }
        $row = CustomerModel::get($id);
        if (empty($row)) {
            $this->error('数据不存在');
        }
        $row->is_black = 1;
        $row->black_type = $params['black_type'];
        $row->black_reason = $params['black_reason'];
        $row->save();
        $this->success('成功');
    }

}
